﻿@{
    ViewBag.Title = "消息群发";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Scripts{
<script src="~/Scripts/plugins/layer/layer.min.js"></script>
<script src="~/Scripts/sys/lib/common_raw.js"></script>
    <script>
        (function ($) {
            $.fn.upload = function (options) {
                var settings = {
                    dom: this,
                    action: '',
                    upload_type: '',
                    params: {},
                    onChooseFile: false,
                    onSubmit: false
                };
                settings = $.extend(settings, options);
                var width = $(settings.dom).width();
                var height = $(settings.dom).height();

                var file = $('<input id="file" name="file" type="file"/>');
                file.attr({ accept: "image/jpeg,image/png", name: settings.upload_type });
                if (settings.onSubmit) {
                    var iFrameName = "upload_icon_iframe",
                        form = $('<div></div>'),
                        iFrame = $("<div style='display:none;'></div>");
                    iFrame.attr({ name: iFrameName, id: iFrameName });
                    form.attr({ action: settings.action, enctype: "multipart/form-data", method: "post", target: iFrameName });
                    for (var param in settings.params) {
                        var input = $("<input type='hidden'/>").attr({ name: param, value: settings.params[param], id: param });
                        form.append(input);
                        input = null;
                    }
                    file.appendTo(form);
                    form.append(iFrame);

                    iFrame.on('load', function () {
                        var resp = $(this.contentDocument.body).text();
                        if (resp) {
                            resp = $.parseJSON(resp);
                            settings.onSubmit(resp);
                        }
                    });

                    file.css({
                        'position': 'absolute',
                        width: '100%',
                        height: '100%',
                        left: 0,
                        top: 0,
                        right: 0,
                        bottom: 0,
                        opacity: 0
                    });
                    form.css({ 'position': 'absolute', width: '100%', height: '100%', left: 0, top: 0, right: 0, bottom: 0 });
                    $(settings.dom).css({ 'position': 'relative', overflow: 'hidden' });
                    $(settings.dom).append(form);
                } else {
                    file.css({
                        'position': 'absolute',
                        width: '100%',
                        height: '100%',
                        left: 0,
                        top: 0,
                        right: 0,
                        bottom: 0,
                        opacity: 0
                    });
                    $(settings.dom).css({ 'position': 'relative', overflow: 'hidden' }).append(file);
                }

                file.on('change', function () {
                    var flg = false;
                    if (this.files[0] && this.value.toLocaleString().match('^.*\.[jpg|png]$')) {
                        if (settings.onChooseFile) {
                            var img = new Image();
                            window.URL = window.URL || window.webkitURL;
                            if (window.URL) {
                                img.src = window.URL.createObjectURL(this.files[0]);
                                img.onload = function (e) {
                                    window.URL.revokeObjectURL(this.src);
                                };
                            } else {
                                if (this.files[0].getAsDataURL) {
                                    img.src = this.files[0].getAsDataURL();
                                    iFrame.contentDocument.body.text(img.src);
                                }
                            }
                            settings.onChooseFile(img, this);
                        }
                        if (settings.onSubmit) {


                            var formData = new FormData();
                            formData.append("file", $("#file")[0].files[0]);
                            $.ajax({
                                url: "http://changyan.sohu.com/api/2/comment/attachment",
                                type: 'POST',
                                data: formData,
                                processData: false,
                                contentType: false,
                                beforeSend: function () {
                                    layer.open({
                                        content: '正在上传.....',
                                        style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                                        time: 2,
                                        shade: false
                                    });
                                },
                                success: function (responseStr) {
                                    var json = eval("(" + responseStr + ")");
                                    $("#picUrl").val(json.url);

                                    //$("#file_img01").attr("src", json.url);

                                },
                                error: function (responseStr) {
                                    layer.open({
                                        content: '错误，请重新上传.....',
                                        style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                                        time: 2,
                                        shade: false
                                    });

                                }
                            });
                        }
                    }
                })
            }
        })(jQuery)
        $(function () {
            uploadImg();
            $('#sendMsg').click(function () {
                common.invokeApi(
                "POST",
                "/Role/PostMessage",
                { AU_NameS: $('#AU_NameS').val(), picUrl: $('#picUrl').val(), sendContent: $('#sendContent').val() },
                function () { },
                function (data) {
                    if (data.State) {

                        common.msg(data.Message, 1, function () {
                            window.location.href = "/message.html";
                            //parent.location.reload();
                            //parent.layer.close(index);
                        });
                    }
                    else {
                           
                        common.msg(data.Message, 2, function () { });
                    }
                },
                function (data) {
                    common.msg(data.Message, 2, function () { });
                },
                function () { }
                );

            })
            
        })

        function uploadImg() {
             
            $("#upload_avatar_btn").upload({
                action: 'http://changyan.sohu.com/api/2/comment/attachment',
                upload_type: 'avatar',
                onChooseFile: function (img) {
                   // $("#picUrl").attr('src', img.src);
                    layer.open({
                        content: '正在上传.....',
                        style: 'background:rgba(0,0,0, .6); color:#fff; border:none;',
                        time: 2,
                        shade: false
                    });
                },
                onSubmit: function (resp) {

                }
            });

        }

    </script>
}
<div class="row">
    <div class="col-lg-12">
        <div class="row">
            <div class="col-lg-12">
                <div class="main-box">
                    <header class="main-box-header clearfix">
                        <h2>消息群发</h2>
                    </header>
                    <div class="main-box-body clearfix">
                        <form role="form">
                            <div class="form-group">

                                <input type="text" class="form-control" id="AU_NameS" placeholder="指定群发代理账号，多个账号用逗号分隔">
                            </div>

                            <div class="form-group">

                                <div class="radio">
                                    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked="">
                                    <label for="optionsRadios1">
                                        微信发单客户端
                                    </label>
                                </div>
                                @*<div class="radio">
                                        <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
                                        <label for="optionsRadios2">
                                            QQ发单客户端
                                        </label>
                                    </div>*@
                            </div>
                            <div class="form-group">

                                <div class="input-group">
                                    <input type="text" id="picUrl" name="search" placeholder="群发消息图片URL地址" class="form-control input-lg" />
                                    <div class="input-group-btn">
                                        <a href="#" id="upload_avatar_btn" class="btn btn-primary"> <span class="glyphicon glyphicon-download-alt"></span>上传 </a>
                                        @*<button class="btn btn-lg btn-primary" type="button">
                                                <i class="fa fa-search"></i> 上传
                                            </button>*@
                                    </div>
                                </div>

                            </div>

                            @*<div class="form-group">

                                    <input type="text" id="name" name="name" value="" class="form-control" placeholder="群发消息图片URL地址">
                                    <input id="fileUpImg_1" name="fileUpImg_1"  data-bfi-disabled='' type="file" />
                                </div>*@
                            <div class="form-group">

                                <textarea class="form-control" id="sendContent" placeholder="群发消息的文本内容" rows="10"></textarea>
                            </div>

                            <div class="form-group">
                                <button type="button" id="sendMsg" class="btn btn-success btn-lg">
                                    &nbsp; &nbsp; 群发消息&nbsp; &nbsp;
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

